type Payload = Record<string, unknown>

interface DragBus {
  dragNode: HTMLElement | null
  payload: Payload | null
  setDrag(node: HTMLElement, data: Payload): void
  reset(): void
}

// 新增属性更新事件相关类型
interface PropUpdateEvent {
  index: number
  data: Record<string, unknown>
}

type PropUpdateCallback = (event: PropUpdateEvent) => void

class EventBus {
  private callbacks: PropUpdateCallback[] = []

  subscribe(callback: PropUpdateCallback) {
    this.callbacks.push(callback)
    return () => {
      const index = this.callbacks.indexOf(callback)
      if (index > -1) {
        this.callbacks.splice(index, 1)
      }
    }
  }

  emit(event: PropUpdateEvent) {
    this.callbacks.forEach(callback => callback(event))
  }
}

export const propUpdateBus = new EventBus()

export const dragBus: DragBus = {
  dragNode: null,
  payload: null,
  setDrag(node, data) {
    this.dragNode = node
    this.payload = data
  },
  reset() {
    this.dragNode = null
    this.payload = null
  },
}